
var Application = Class.extend(App = {
    membersController: null,
    controller: null,
    
    init: function() {
        this.bindTabs();
        this.consume_alert();
        this.consume_confirm();
        
        $(".nano").nanoScroller();
    },
            
    bindTabs: function() {
        var self = this;

        $('a[data-toggle="tab"]').on('shown', function(e) {
            if ($(e.target).attr('href') == "#members" && self.membersController == null) {
                self.membersController = new MembersController();
            }
        });
    },
            
    setController: function(controller) {
        this.controller = controller;
    },
            
    getController: function() {
        return this.controller;
    },
            
    consume_alert: function() {
        if ( typeof _alert != 'undefined' ) return;
        
        _alert = window.alert;
        window.alert = function(message) {
            $.pnotify({
                title: 'Alert',
                text: message
            });
        };
    },
            
    consume_confirm: function() {
        if( typeof _confirm != 'undefined' ) return;
        
        _confirm = window.confirm;
        
        window.confirm = function(message, callback) {
            
            if( callback == null ) {
                return _confirm(message);
            }
            
            var confirmHTML = "<div id='pn_confirm'><p style='margin: 10px;'>%message%</p>"
                        + "<p align=center><input type='button' class='btn btn-primary' value='OK' /> <input type='button' class='btn btn-cancel' value='Cancel' /></p>"
                        + "</div>";
                        
            confirmHTML = confirmHTML.replace("%message%", message);
            
            var modal_overlay;
            
            var confirmNotice = $.pnotify({
                title: 'Confirm',
                text: confirmHTML,
                hide: false,
                delay: 20000,
                history: false,
                stack: false,
                width: '450',
                before_open: function(pnotify) {
                    // Position this notice in the center of the screen.
                    pnotify.css({
                        top: ($(window).height() / 2) - (pnotify.height() / 2),
                        left: ($(window).width() / 2) - (pnotify.width() / 2)
                    });
                    // Make a modal screen overlay.
                    if (modal_overlay) modal_overlay.fadeIn("fast");
                    else modal_overlay = $("<div />", {
                        class: "ui-widget-overlay",
                        css: {
                            display: "none",
                            position: "fixed",
                            top: "0",
                            bottom: "0",
                            right: "0",
                            left: "0"
                        }
                    }).appendTo("body").fadeIn("fast");
                },
                before_close: function() {
                    modal_overlay.fadeOut("fast");
                }
            });
            
            $("#pn_confirm .btn-primary").click(function() {
                confirmNotice.pnotify_remove();
                
                if( callback ) {
                    callback();
                }
            });
            
            $("#pn_confirm .btn-cancel").click(function() {
                confirmNotice.pnotify_remove();
            });
        };
    }
});

App.UI = {
    showDialog: function(options) {
        var _default = {
            title: 'Dialog Title',
            width: '500',
            height: '500',
            content: ''
        };

        _default = $.extend(_default, options);

        $("#app_dialog").dialog(_default).html(_default['content']);
        $("#app_dialog").dialog('open');
        $("#app_dialog").parent().find(".ui-dialog-buttonpane button").addClass("btn");
    },
          
    hideDialog: function() {
        $("#app_dialog").dialog('close');
    },
            
    setAjaxContent: function(content) {
        $("#ajax-content").html(content);
    },
    showContentPane: function(show) {
        if (show) {
            $("#content-row").hide();
            $("#ajax-content").show('fast');
        }

        else {
            $("#content-row").show('fast');
            $("#ajax-content").hide();
        }


    }
};

$(document).ready(function() {
    var App = new Application();
});